package com.example.testluckyroulette.widget

import android.content.Context
import android.graphics.*
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.example.testluckyroulette.R
import com.example.testluckyroulette.bean.BitmapCallBack
import com.example.testluckyroulette.bean.BitmapNoStatusCallBack
import com.example.testluckyroulette.bean.LuckyPersonBean

/**
 * Create Time:2020/9/11
 * Author: Lindanrong
 * Description:用于圆盘上面的头像的view
 */
class TestMatrixUser : View {

    var isPend = false
    private val DEFAULT_BORDER_WIDTH = 4
    private val DEFAULT_BORDER_COLOR = Color.BLACK
    private val mBorderColor: Int = DEFAULT_BORDER_COLOR
    private val mBorderWidth: Int = DEFAULT_BORDER_WIDTH
    var mRadio = 0
    //头像的圆的矩阵
    private val mDrawableRect = RectF()
    //头像的背景圆的矩阵
    private val mBorderRect = RectF()
    //头像的画笔笔头
    private var mBitmapShader: BitmapShader? = null
    //头像的矩阵
    private val mShaderMatrix = Matrix()
    //头像的画笔
    private val mBitmapPaint = Paint()
    //头像背景圆的画笔
    private val mBorderPaint = Paint()

    var mContext: Context
    var mWith: Int = 0
    var mHeight: Int = 0
    var url1 = "https://img.q17z.com/FvCHQaXeyCf-6go1vPsJWj8Knbe"
    var url2 = "https://img.q17z.com/picture_1594275736284.jpg?imageslim"
    lateinit var l:LuckyPersonBean
    lateinit var l2:LuckyPersonBean
    var mHandler =object :Handler(Looper.getMainLooper()){
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            isPend = true
            invalidate()
        }
    }
    var i = 0

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {
        mContext = context
        init()
    }

    private fun init() {
        mBitmapPaint.style = Paint.Style.FILL
        l = LuckyPersonBean().apply {
            setAvatar(mContext ,
                "https://img.q17z.com/picture_1594275736284",//.jpg?imageslim
                1) {
                i++
                mHandler.sendEmptyMessage(1)
            }
            name = "lin1"
            color = R.color.colorAccent1
        }
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWith = MeasureSpec.getSize(widthMeasureSpec)
        mHeight = MeasureSpec.getSize(heightMeasureSpec)
        mRadio = mWith.coerceAtMost(mHeight) / 2  //求出两个值哪个比较小
        //确定一下圆心的坐标
    }

    override fun onDraw(canvas: Canvas) {
        if (!isPend){
            return
        }
        super.onDraw(canvas)

        setup(l.avatarBitmap)
        canvas.drawCircle(mDrawableRect.centerX(),mDrawableRect.centerY(),mRadio/4f+mBorderWidth,mBorderPaint)
        canvas.drawCircle(mDrawableRect.centerX(),mDrawableRect.centerY(),mRadio/4f,mBitmapPaint)
    }

    private fun setup(oldbitmap: Bitmap) {
        mBitmapShader = BitmapShader(
            oldbitmap,
            Shader.TileMode.CLAMP,
            Shader.TileMode.CLAMP
        )
        mBitmapPaint.apply {
            isAntiAlias = true
            shader = mBitmapShader
        }
        mBorderPaint.apply {
            style = Paint.Style.FILL
            isAntiAlias = true
            color = mBorderColor
        }
        //设置背景 + 头像的位置
        mBorderRect.set(mRadio*3/4f, mRadio/4f, mRadio+mRadio/4f, mRadio/4f+mRadio/2f)
        mDrawableRect.set(mRadio*3/4f, mRadio/4f, mRadio+mRadio/4f, mRadio/4f+mRadio/2f)
        updateShaderMatrix(oldbitmap)
    }

    /**
     * 对图片进行缩小跟移动中心点
     */
    private fun updateShaderMatrix(oldbitmap: Bitmap) {
        mShaderMatrix.set(null)
        val scale = mDrawableRect.width() / oldbitmap.width.toFloat()
        var dx = (mDrawableRect.width() - oldbitmap.width * scale) * 0.5f
        var dy = (mDrawableRect.height() - oldbitmap.height * scale) * 0.5f
        mShaderMatrix.setScale(scale, scale)
        mShaderMatrix.postTranslate(
            (dx + 0.5f).toInt()+/**起始位置也要加上**/mRadio*3/4f,
            (dy + 0.5f).toInt()+/**起始位置也要加上**/mRadio/4f
        )
        mBitmapShader!!.setLocalMatrix(mShaderMatrix)
    }

}

